package org.sam.swing;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 字体属性设置对话框
 * 
 * @author sam
 *
 */
@SuppressWarnings("serial")
public class JSFontChooser extends JPanel {

	// begin 定义变量
	/**
	 * 当前的字体名称,默认宋体.
	 */
	private String current_fontName;

	/**
	 * 当前的字样,默认常规.
	 */
	private int current_fontStyle;

	/**
	 * 当前字体大小,默认9号.
	 */
	private int current_fontSize;
	private JLabel lblFont; // 选择字体的LBL
	private JLabel lblStyle; // 选择字型的LBL
	private JLabel lblSize; // 选择字大小的LBL
	private JLabel lblColor; // 选择Color的label
	private Button buttonColor; // 颜色选择
	private JTextField txtFont; // 显示选择字体的TEXT
	private JTextField txtStyle; // 显示选择字型的TEXT
	private JTextField txtSize; // 显示选择字大小的TEXT
	private JTextField showTF; // 展示框（输入框）
	private JList<String> lstFont; // 选择字体的列表.
	private JList<String> lstStyle; // 选择字型的列表.
	private JList<String> lstSize; // 选择字体大小的列表.
	private JScrollPane spFont;
	private JScrollPane spSize;
	private JPanel showPan; // 显示框.
	private Map<String, Integer> sizeMap; // 字号映射表.
	private Map<String, Color> colorMap; // 字着色映射表.
	// end

	// 重载构造，有参的初始化 用于初始化字体界面
	public JSFontChooser(Font font, Color color) {
		this.current_fontName = font.getName();
		this.current_fontSize = font.getSize();
		this.current_fontStyle = font.getStyle();
		/* 初始化界面 */
		init(font, color);
	}

	/**
	 * 带有默认值的构造函数
	 */
	public JSFontChooser() {
		this(Font.decode("宋体-plain-9"), Color.WHITE);
	}

	// 可供外部调用的方法
	public Font getSelectedfont() {
		return new Font(current_fontName, current_fontStyle, current_fontSize);
	}

	public Color getSelectedcolor() {
		return this.buttonColor.getBackground();
	}

	/**
	 * 设置文本选中颜色
	 * 
	 * @param color
	 */
	public void setSelectedColor(Color color) {
		if (color != null) {
			this.buttonColor.setBackground(color);
		}
	}

	/* 初始化界面 */
	private void init(Font font, Color color) {
		// 实例化变量
		lblFont = new JLabel("字体:");
		lblStyle = new JLabel("字型:");
		lblSize = new JLabel("大小:");
		lblColor = new JLabel("颜色:");
		buttonColor = new Button();
		txtFont = new JTextField("宋体");
		txtStyle = new JTextField("常规");
		txtSize = new JTextField("9");

		// 取得当前环境可用字体.
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fontNames = ge.getAvailableFontFamilyNames();

		lstFont = new JList<String>(fontNames);

		// 字形.
		lstStyle = new JList<String>(new String[] { "常规", "粗体", "斜体", "粗斜体" });

		// 字号.
		String[] sizeStr = new String[] { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28",
				"36", "48", "72", "初号", "小初", "一号", "小一", "二号", "小二", "三号", "小三", "四号", "小四", "五号", "小五", "六号", "小六",
				"七号", "八号" };
		int sizeVal[] = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 42, 36, 26, 24, 22, 18, 16, 15,
				14, 12, 11, 9, 8, 7, 6, 5 };
		sizeMap = new HashMap<String, Integer>();
		for (int i = 0; i < sizeStr.length; ++i) {
			sizeMap.put(sizeStr[i], sizeVal[i]);
		}
		lstSize = new JList<String>(sizeStr);
		spFont = new JScrollPane(lstFont);
		spSize = new JScrollPane(lstSize);

		// 颜色
		String[] colorStr = new String[] { "黑色", "蓝色", "青色", "深灰", "灰色", "绿色", "浅灰", "洋红", "桔黄", "粉红", "红色", "白色",
				"黄色" };
		Color[] colorVal = new Color[] { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN,
				Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
		colorMap = new HashMap<String, Color>();
		for (int i = 0; i < colorStr.length; i++) {
			colorMap.put(colorStr[i], colorVal[i]);
		}
		showPan = new JPanel();
		showPan.setBackground(Color.WHITE);

		// 布局控件
		// 字体框
		this.setLayout(null); // 不用布局管理器
		add(lblFont);
		lblFont.setBounds(12, 10, 30, 20);
		txtFont.setEditable(false);
		add(txtFont);
		txtFont.setBounds(12, 30, 155, 20);
		txtFont.setText("宋体");
		lstFont.setSelectedValue("宋体", true);
		txtFont.setText(font.getName());
		lstFont.setSelectedValue(font.getName(), true);

		add(spFont);
		spFont.setBounds(12, 50, 155, 80);

		// 样式
		add(lblStyle);
		lblStyle.setBounds(175, 10, 30, 20);
		txtStyle.setEditable(false);
		add(txtStyle);
		txtStyle.setBounds(177, 30, 130, 20);
		lstStyle.setBorder(javax.swing.BorderFactory.createLineBorder(Color.gray));
		add(lstStyle);
		lstStyle.setBounds(177, 50, 130, 80);
		txtStyle.setText("常规"); // 初始化为默认的样式
		lstStyle.setSelectedValue("常规", true); // 初始化为默认的样式
		if (font != null) {
			lstStyle.setSelectedIndex(font.getStyle()); // 初始化样式list
			if (font.getStyle() == 0) {
				txtStyle.setText("常规");
			} else if (font.getStyle() == 1) {
				txtStyle.setText("粗体");
			} else if (font.getStyle() == 2) {
				txtStyle.setText("斜体");
			} else if (font.getStyle() == 3) {
				txtStyle.setText("粗斜体");
			}
		}

		// 大小
		add(lblSize);
		lblSize.setBounds(320, 10, 30, 20);
		txtSize.setEditable(false);
		add(txtSize);
		txtSize.setBounds(320, 30, 60, 20);
		add(spSize);
		spSize.setBounds(320, 50, 60, 80);
		lstSize.setSelectedValue(Integer.toString(font.getSize()), false);
		txtSize.setText(Integer.toString(font.getSize()));

		// 颜色
		buttonColor.setBounds(52, 226, 42, 22);
		buttonColor.setBackground(color);
		add(buttonColor);
		lblColor.setBounds(12, 226, 30, 20);
		add(lblColor);

		// 展示框
		showTF = new JTextField();
		showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
		showTF.setBounds(10, 10, 300, 50);
		showTF.setHorizontalAlignment(JTextField.CENTER);
		showTF.setText("字符示例: AaBbYyZz");
		showTF.setBackground(Color.white);
		showTF.setEditable(false);
		showPan.setBorder(javax.swing.BorderFactory.createTitledBorder("示例"));
		add(showPan);
		showPan.setBounds(12, 140, 368, 80);
		showPan.setLayout(new BorderLayout());
		showPan.add(showTF);

		if (font != null) {
			showTF.setFont(font); // 设置示例中的文字格式
		}
		if (font != null) {
			showTF.setForeground(color);
		}
		// 布局控件_结束

		// listener.....
		/* 用户选择字体 */
		lstFont.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				current_fontName = (String) lstFont.getSelectedValue();
				txtFont.setText(current_fontName);
				showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
			}
		});

		/* 用户选择字型 */
		lstStyle.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				@SuppressWarnings("unchecked")
				String value = (String) ((JList<String>) e.getSource()).getSelectedValue();
				if (value.equals("常规")) {
					current_fontStyle = Font.PLAIN;
				}
				if (value.equals("斜体")) {
					current_fontStyle = Font.ITALIC;
				}
				if (value.equals("粗体")) {
					current_fontStyle = Font.BOLD;
				}
				if (value.equals("粗斜体")) {
					current_fontStyle = Font.BOLD | Font.ITALIC;
				}
				txtStyle.setText(value);
				showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
			}
		});

		/* 用户选择字体大小 */
		lstSize.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				current_fontSize = (Integer) sizeMap.get(lstSize.getSelectedValue());
				txtSize.setText(String.valueOf(current_fontSize));
				showTF.setFont(new Font(current_fontName, current_fontStyle, current_fontSize));
			}
		});

		/* 其它颜色 */
		buttonColor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("static-access")
				Color col_temp = new JColorChooser().showDialog(null, null, color);
				if (col_temp != null) {
					showTF.setForeground(col_temp);
					buttonColor.setBackground(col_temp);
				}
			}
		});
	}

	/**
	 * 字体样式
	 */
	private static final Map<Integer, String> FONT_STYLES = new HashMap<>();

	/**
	 * 初始化数据
	 */
	static {
		FONT_STYLES.put(Font.PLAIN, "plain");
		FONT_STYLES.put(Font.BOLD, "blod");
		FONT_STYLES.put(Font.ITALIC, "italic");
		FONT_STYLES.put(Font.ITALIC | Font.BOLD, "bolditalic");
	}

	/**
	 * 获取编码后的字符串
	 * 
	 * @return
	 */
	public String getFontDecode() {
		return this.current_fontName + "-" + FONT_STYLES.get(current_fontStyle) + "-" + this.current_fontSize;
	}
}
